var UpdatePositionForm = function () {
  var template = /* html */ `
    <div class="update-position-form" v-loading.fullscreen.lock="loading">
      <div class="form-content">
        <div v-for="(position,index) in positionList" :key="'position_'+index" class="position-item">
          <el-input size="medium" v-model="position.name" placeholder="请输入仓位"></el-input>
          <el-button v-if="positionList.length>1" size="medium" type="text" title="删除" @click="delPosition(index)">
            <i class="el-icon-delete"></i>
          </el-button>
        </div>
      </div>
      <div class="form-btns">
        <el-button size="medium" type="primary" @click="addPosition">添加仓位</el-button>
      </div>
      <div style="text-align:center;margin-top:10px">
        <el-button type="primary" @click="updatePosition">修改</el-button>
      </div>
    </div>
  `;

  return {
    props: {
      warehouse: {
        type: Object,
        required: true
      }
    },

    data: function () {
      var positionList = this.warehouse.positionList;
      positionList = _.map(positionList, function (position) {
        return {
          name: position
        };
      })
      return {
        loading: false,
        positionList: positionList
      };
    },

    methods: {
      addPosition: function () {
        this.positionList.push({
          name: ''
        });
      },

      delPosition: function (index) {
        this.positionList.splice(index, 1);
      },

      updatePosition: function () {
        var positionList = this.positionList;
        if (positionList.length === 0) {
          this.$alert('物料仓位个数不能为0', '信息');
          return;
        }

        var positions = [];
        for (var i = 0; i < positionList.length; i++) {
          var position = positionList[i];
          var name = position.name;
          name = name.trim();
          if (name === '') {
            this.$alert('请输入仓位', '信息');
            return;
          }
          positions.push(name);
        }

        var uniqPositions = _.uniq(positions);
        if (uniqPositions.length !== positions.length) {
          this.$alert('仓位名称有重复', '信息');
          return;
        }

        var warehouse = this.warehouse;
        var materialCode = warehouse.materialCode;
        positions = _.map(positions, function (position) {
          return {
            materialCode: materialCode,
            position: position
          };
        });

        this.loading = true;
        var self = this;
        ajaxRequest("materialPosition/updateMaterialPositionList", {
          materialPositionData: JSON.stringify(positions)
        }).then(function () {
          self.loading = false;
          self.$alert('物料仓位修改成功', '信息', {
            callback: function () {
              self.$emit('refresh-data', true);
            }
          });
        }).catch(function () {
          self.loading = false;
        });
      }
    },

    template: template
  };
}();